package xjtu.se.articlecommunity.admin.login;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xjtu.se.articlecommunity.admin.login.dto.Mail;
import xjtu.se.articlecommunity.admin.login.dto.Admin;
import xjtu.se.articlecommunity.admin.login.dto.Tel;
import xjtu.se.articlecommunity.common.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/login")
public class AdminLoginAPI {

    @Resource
    private AdminLoginService loginService;

    @Resource
    private MailService mailService;

    @Resource
    private SMSService smsService;

    @Resource
    private CacheService cacheService;

    @RequestMapping("/byPwd")
    private Result loginByPwd(@RequestBody Admin admin) {

        if(loginService.loginByPwd(admin)){
            String token = TokenUtils.loginSign(admin.getU_id(),admin.getU_pwd(),2);
            return Result.success((Object)token);
        }

        return Result.fail(Result.ERR_CODE_BUSINESS, "账号或密码错误！");
    }

    @RequestMapping("/getMailVerificationCode/{mail}")
    private Result getMailVerificationCode(@PathVariable String mail) {
        if(!loginService.checkMail(mail)) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "该邮箱未绑定 admin 账号！");
        }

        String code = mailService.sendCode(mail);
        cacheService.saveVerificationCode(mail, code);
        return Result.success();
    }

    @RequestMapping("/byMailVerification")
    private Result loginByMailVerificationCode(@RequestBody Mail mail) {
        if(!loginService.checkMail(mail.getU_mail())) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "该邮箱未绑定 admin 账号！");
        }
        String code = cacheService.findVerificationCode(mail.getU_mail());
        if(code.equals(mail.getCode())){
            Admin admin = loginService.getInfoByMail(mail);
            String token = TokenUtils.loginSign(admin.getU_id(), admin.getU_pwd(), 2);
            return Result.success((Object) token);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS, "验证码失效或错误！");
    }

    @RequestMapping("/getTelVerificationCode/{tel}")
    private Result getTelVerificationCode(@PathVariable String tel) {
        if(!loginService.checkTel(tel)) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "该手机号未绑定 admin 账号！");
        }
        String code = smsService.sendVerificationCode(tel);
        cacheService.saveVerificationCode(tel, code);
        return Result.success();
    }

    @RequestMapping("/byTelVerification")
    private Result loginByTelVerificationCode(@RequestBody Tel tel) {
        if(!loginService.checkTel(tel.getU_tel())) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "该手机号未绑定 admin 账号！");
        }
        String code = cacheService.findVerificationCode(tel.getU_tel());
        if(code.equals(tel.getCode())){
            Admin admin = loginService.getInfoByTel(tel);
            String token = TokenUtils.loginSign(admin.getU_id(), admin.getU_pwd(), 2);
            return Result.success((Object) token);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS, "验证码失效或错误！");
    }
}
